﻿namespace DesignModeConsole.行为型.迭代器模式;

public class IteratorService : IService
{
    public void Main()
    {
        // 迭代器示例
        var nameList = new List<string>
        {
            "张三",
            "李四",
            "王五"
        };

        // 将需要遍历的容器对象通过构造函数的形式传递给迭代器类
        ITerator<string> iterator = new ArrayIterator<string>(nameList);
        while (iterator.HasNext())
        {
            Console.WriteLine(iterator.CurrentItem());
            iterator.Next();

            // 修改集合
            //nameList.Add("赵六");
        }

        // 示例3
        IList3<string> list = new ArrayList3<string>();
        list.Add("3");
        list.Add("8");
        list.Add("2");
        var iter1 = list.Iterator();// 快照 3 8 2
        list.Remove("2");
        var iter2 = list.Iterator();// 快照 3 8
        list.Remove("3");
        var iter3 = list.Iterator();// 快照 3

        // 输出结果
        Console.WriteLine("iter1输出结果");
        while (iter1.HasNext())
        {
            var item = iter1.Next();
            Console.WriteLine(item);
        }

        Console.WriteLine("iter2输出结果");
        while (iter2.HasNext())
        {
            var item = iter2.Next();
            Console.WriteLine(item);
        }

        Console.WriteLine("iter3输出结果");
        while (iter3.HasNext())
        {
            var item = iter3.Next();
            Console.WriteLine(item);
        }
    }
}